/* eslint-disable no-loss-of-precision */
/**
 * @author: SEKin 
 * @Date: 2025-03-12 09:19:46 
 * @description:  JavaScript 记忆化
 * @Last Modified time: 2025-03-12 09:19:46 
 */
import {AnyObj} from '../../common'

function square(num: number) {
	let count = 0;
  while(count < num){
    count++
	}
	return count;
}
const testNum = 999999999;
console.log(square(2)); // 返回 4
console.time("1")
console.log(square(testNum)); // 这是一项耗时的计算
console.timeEnd("1");
alert(square(testNum));

function memoizedSquare() {
  let cache: AnyObj= {};
  return function optimizedSquare(num:number) {
    if (num in cache) {
      return cache[num];
    } else {
      const result = square(num);
      cache[num] = result;
      return result;
    }
  };
}
const memoSquare = memoizedSquare();

console.time('2')
console.log(memoSquare(testNum));
console.timeEnd("2");
